// index.js
// 获取应用实例
function getRandomColor() {
    let rgb = [];
    for (let i = 0; i < 3; ++i) {
        let color = Math.floor(Math.random() * 256).toString(16)
        color = color.length == 1 ? '0' + color : color
        rgb.push(color)
    }
    return '#' + rgb.join('')
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [{
            id: '1001',
            title: '艾草种植视频介绍1',
            image: "../../../static/floor/Video.png",
            year: "2024",
            description: "描述了艾草的种植...",
            videoUrl: 'http://scxzd87lz.hn-bkt.clouddn.com/videoTest.mp4?e=1714794638&token=t-wcPGU-BFD1627D5rtDtbqD6XG-sjihHwOjaYzV:czDnxceSgKzen0g_WbNmuUddki8='
        },
        {
            id: '1002',
            title: '艾草种植视频介绍2',
            image: "../../../static/floor/Video.png",
            year: "2024",
            description: "描述了艾草的种植...",
            videoUrl: 'http://scxzd87lz.hn-bkt.clouddn.com/videoTest.mp4?e=1714794638&token=t-wcPGU-BFD1627D5rtDtbqD6XG-sjihHwOjaYzV:czDnxceSgKzen0g_WbNmuUddki8='
        },
        {
            id: '1003',
            title: '艾草种植视频介绍3',
            image: "../../../static/floor/Video.png",
            year: "2024",
            description: "描述了艾草的种植...",
            videoUrl: 'http://scxzd87lz.hn-bkt.clouddn.com/videoTest.mp4?e=1714794638&token=t-wcPGU-BFD1627D5rtDtbqD6XG-sjihHwOjaYzV:czDnxceSgKzen0g_WbNmuUddki8='
        },
        {
            id: '1004',
            title: '艾草种植视频介绍4',
            image: "../../../static/floor/Video.png",
            year: "2024",
            description: "描述了艾草的种植...",
            videoUrl: 'http://scxzd87lz.hn-bkt.clouddn.com/videoTest.mp4?e=1714794638&token=t-wcPGU-BFD1627D5rtDtbqD6XG-sjihHwOjaYzV:czDnxceSgKzen0g_WbNmuUddki8='
        },
        {
            id: '1005',
            title: '艾草种植视频介绍5',
            image: "../../../static/floor/Video.png",
            year: "2024",
            description: "描述了艾草的种植...",
            videoUrl: 'http://scxzd87lz.hn-bkt.clouddn.com/videoTest.mp4?e=1714794638&token=t-wcPGU-BFD1627D5rtDtbqD6XG-sjihHwOjaYzV:czDnxceSgKzen0g_WbNmuUddki8='
        },
        {
            id: '1006',
            title: '艾草种植视频介绍6',
            image: "../../../static/floor/Video.png",
            year: "2024",
            description: "描述了艾草的种植...",
            videoUrl: 'http://scxzd87lz.hn-bkt.clouddn.com/videoTest.mp4?e=1714794638&token=t-wcPGU-BFD1627D5rtDtbqD6XG-sjihHwOjaYzV:czDnxceSgKzen0g_WbNmuUddki8='
        }
        ],
        //视频路径
        src: "",
        //弹幕颜色
        color: "",
        //弹幕内容
        danmuText: ""
    },
    onLoad: function (options) {
        this.videoCtx = wx.createVideoContext('myVideo')
    },

    playVideo: function (e) {
        this.videoCtx.stop();
        this.setData({
            src: e.currentTarget.dataset.url
        })
        this.videoCtx.play()
    },

    /* 更新弹幕内容 */
    getDanmu: function (e) {
        this.setData({
            danmuText: e.detail.value
        })
    },

    /* 发送弹幕 */
    sendDanmu: function (e) {
        let text = this.data.danmuText;
        this.videoCtx.sendDanmu({
            text: text,
            color: getRandomColor()
        })
    }
})